public class test5 {
    public static void main(String[] args) {
        Node a = new Node(1);
        Node b = new Node(3);
        Node c = new Node(4);
        Node d = new Node(6);
        Node e = new Node(7);

        a.next = b;
        b.next = e;

        c.next = d;
        d.next = e;

        Node result = getIntersectionNode(a,c);
        System.out.println(result.val);
    }

    public static Node getIntersectionNode(Node headA, Node headB) {

        //先遍历到最后,再往前遍历找同一个点
        if(headA==null || headB == null){
            return null;
        }
        if(headA == headB){
            return headA;
        }
        Node cur1 = headA.next;
        Node cur2 = headB.next;
        Node node1 = headA;
        Node node2 = headB;
        //处理链表1
        while(node1.next!=null){
            node1.next = cur1.next;
           cur1.next = headA;
           headA = cur1;
           cur1 = node1.next;
        }
        //处理链表2
        while(node2.next!=null){
            node2.next = cur2.next;
            cur2.next = headB;
            headB = cur2;
            cur2 = node2.next;
        }
        //从后往前走
        while(cur1!=null && cur2!=null){
            if(cur1!=cur2)
                return cur1;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return null;
    }
}
